#import "DDExchangeViewController.h"
#import "DDExchangeView.h"
@interface DDExchangeViewController ()
@property (nonatomic, strong) DDTextField *nameTF;
@property (nonatomic, strong) DDButton *btn;
@property (nonatomic, strong) DDExchangeView *exchangeView;
@property (nonatomic, strong) DDButton *jixuBtn;
@end
@implementation DDExchangeViewController
- (void)viewDidLoad {
    [super viewDidLoad];
}
- (DDTextField *)nameTF
{
    if (!_nameTF)
    {
        _nameTF = [[DDTextField alloc] init];
        _nameTF.type = JTWTextFieldTypeDefault;
        [self.ddview addSubview:_nameTF];
        _nameTF.c_left.equal(self.ddview).with.coffset(GAP);
        _nameTF.c_right.equal(self.ddview).with.coffset(-GAP);
        _nameTF.c_height.equal(TEXTFILDHEIGHT);
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            _nameTF.c_bottom.equal(self.ddview.c_centerY).with.coffset(- GAP * 2);
        else
            _nameTF.c_top.equal(self.ddview.bar.c_bottom).with.coffset(GAP);
    }
    return _nameTF;
}
- (DDButton *)btn
{
    if (!_btn)
    {
        _btn = [[DDButton alloc] init];
        _btn.backgroundColor = helpermanger().getPara().color;
        _btn.layer.cornerRadius = GAP / 2.0;
        [_btn addTarget:self action:@selector(yjd_actionForExchangeBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.ddview addSubview:_btn];
        _btn.c_left.equal(self.nameTF);
        _btn.c_right.equal(self.nameTF);
        _btn.c_height.equal(BUTTONHEIGHT);
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            _btn.c_top.equal(self.ddview.c_centerY).with.coffset(GAP * 2);
        else
            _btn.c_top.equal(self.nameTF.c_bottom).with.coffset(GAP);
    }
    return _btn;
}
- (DDButton *)jixuBtn
{
    if (!_jixuBtn)
    {
        _jixuBtn = [[DDButton alloc] init];
        _jixuBtn.backgroundColor = helpermanger().getPara().color;
        _jixuBtn.layer.cornerRadius = GAP / 2.0;
        _jixuBtn.title = @"继续兑换";
        _jixuBtn.hidden = YES;
        [_jixuBtn addTarget:self action:@selector(yjd_actionForJiXuExchangeBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.ddview addSubview:_jixuBtn];
        _jixuBtn.c_top.equal(self.exchangeView.c_bottom).with.coffset(GAP);
        _jixuBtn.c_left.equal(self.exchangeView);
        _jixuBtn.c_right.equal(self.exchangeView);
        _jixuBtn.c_height.equal(BUTTONHEIGHT);
    }
    return _jixuBtn;
}
- (DDExchangeView *)exchangeView
{
    if (!_exchangeView)
    {
        _exchangeView = [[DDExchangeView alloc] init];
        _exchangeView.hidden = YES;
        [self.ddview addSubview:_exchangeView];
        _exchangeView.c_top.equal(self.ddview.bar.c_bottom).with.coffset(GAP);
        _exchangeView.c_left.equal(self.ddview).with.coffset(GAP);
        _exchangeView.c_right.equal(self.ddview).with.coffset(-GAP);
        _exchangeView.c_height.equal(80);
    }
    return _exchangeView;
}
- (void)yjd_actionForExchangeBtn
{
    if ([self.nameTF.text length] <= 0) return [CCAlert showMessage:@"请输入兑换码" hidden:nil];
    [CCHud startHud];
    [self.view endEditing:YES];
    [self.request yjd_exchangeRequest:@{@"cdkey":self.nameTF.text}];
}
- (void)yjd_actionForJiXuExchangeBtn
{
    self.exchangeView.hidden = YES;
    self.jixuBtn.hidden = YES;
    self.nameTF.hidden = NO;
    self.btn.hidden = NO;
}
#pragma mark - JTWRequestDelegate
- (void)yjd_didRequestWithReceiveData:(NSDictionary *)dict error:(NSError *)error
{
    [CCHud stopHud];
    if(error)
    {
        if ((error.code != -999 || ![error.localizedDescription isEqualToString:@"cancelled"]))
            [CCAlert showMessage:[NSString stringWithFormat:@"%@",error.localizedDescription] hidden:nil];
        return;
    }
    if (!dict)
        return;
    int code = -1;
    code = [[dict valueForKey:@"code"] intValue];
    NSString *info = [NSString stringWithFormat:@"%@",[dict valueForKey:@"info"]];
    if (code == 0)
    {
        [CCAlert showMessage:@"兑换成功" hidden:^{
            self.nameTF.hidden = YES;
            self.btn.hidden = YES;
            NSString *limit = [[dict valueForKey:@"coupon"] valueForKey:@"limit"];
            NSString *ID = [[dict valueForKey:@"coupon"] valueForKey:@"id"];
            NSString *expirydate = [[dict valueForKey:@"coupon"] valueForKey:@"expirydate"];
            NSString *gamename = [[dict valueForKey:@"coupon"] valueForKey:@"gamename"];
            NSString *value = [[dict valueForKey:@"coupon"] valueForKey:@"value"];
            NSString *gameid = [[dict valueForKey:@"coupon"] valueForKey:@"gameid"];
            DDExchangeViewPara para = exchangeViewManager().getPara();
            para.title = ([gameid intValue] == 0)?@"平台所有游戏通用":gamename;
            para.date = ([expirydate length] == 0)?@"永久":expirydate;
            para.tips =  ([limit floatValue] == 0)?@"不限金额":[NSString stringWithFormat:@"满足%f元可用",[limit floatValue]];
            para.money = value;
            self.exchangeView.hidden = NO;
            self.jixuBtn.hidden = NO;
            exchangeViewManager().setPara(self.exchangeView,para);
        }];
    }
    else
    {
        [CCAlert showMessage:[NSString stringWithFormat:@"%@" ,info] title:@"错误" enter:nil];
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.ddview.bar.title = @"兑换中心";
    self.nameTF.placeholder = @"请输入兑换码";
    self.btn.title = @"立即兑换";
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
